//
//  CYFDateView.m
//  UnionMerchant
//
//  Created by zhuchao on 16/8/19.
//  Copyright © 2016年 UnionMerchant. All rights reserved.
//



#import "Masonry.h"
#import "CYFDateView.h"
@interface CYFDateView ()
@property(nonatomic,strong)UIView    *contenView;
@property(nonatomic,strong)UIButton  *confirmBtn;
@property(nonatomic,strong)UIButton  *cancelBtn;

@end
@implementation CYFDateView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        [self createSubViews];
    }
    return self;
    
}
- (void)createSubViews{
    
    self.contenView = [[UIView alloc]init];
    self.contenView.backgroundColor = KREDCOLOR;
    [self addSubview:self.contenView];
    
    self.confirmBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    self.confirmBtn.titleLabel.font = [UIFont systemFontOfSize:__I6Width(17)];
    [self.confirmBtn addTarget:self action:@selector(confirmBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.contenView addSubview:self.confirmBtn];
    
    self.cancelBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    self.cancelBtn.titleLabel.font = [UIFont systemFontOfSize:__I6Width(17)];

    [self.cancelBtn addTarget:self action:@selector(cancelBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.contenView addSubview:self.cancelBtn];
    
    self.datePicker = [[UIDatePicker alloc]init];
    //设置显示格式为年月日
    self.datePicker.datePickerMode = UIDatePickerModeDate;
    self.datePicker.backgroundColor = [UIColor whiteColor];
    [self.datePicker setCalendar:[NSCalendar currentCalendar]];
    //设置显示为中文 修复iOS11 显示月份为英文问题
    self.datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];

    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-mm-dd"];
    //NSString *dateString = [dateFormatter stringFromDate:currentDate];
    NSDate* maxDate = currentDate;
    self.datePicker.maximumDate = maxDate;
    [self.contenView addSubview:self.datePicker];
    
    
}
- (void)layoutSubviews{
    
    [self.contenView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.mas_bottom).with.offset(0);
        make.left.mas_equalTo(self.mas_left).with.offset(0);
        make.right.mas_equalTo(self.mas_right).with.offset(0);
        make.height.mas_equalTo(__I6Height(260));
    }];
    
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contenView.mas_top).with.offset(__I6Height(0));
        make.left.mas_equalTo(self.contenView.mas_left).with.offset(__I6Width(5));
        make.width.mas_equalTo(__I6Width(40));
        make.height.mas_equalTo(__I6Height(40));
    }];
    
    
    [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contenView.mas_top).with.offset(__I6Height(0));
        make.right.mas_equalTo(self.contenView.mas_right).with.offset(__I6Width(-5));
        make.width.mas_equalTo(__I6Width(40));
        make.height.mas_equalTo(__I6Height(40));
    }];
    
    [self.datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.contenView.mas_bottom).with.offset(0);
        make.left.mas_equalTo(self.contenView.mas_left).with.offset(0);
        make.right.mas_equalTo(self.contenView.mas_right).with.offset(0);
        make.height.mas_equalTo(__I6Height(220));
    }];

    
}

#pragma mark -- 点击确定按钮
- (void)confirmBtnClicked:(UIButton *)btn{
    if ([_delegate respondsToSelector:@selector(dateView:didSelectedDate:)]) {
        [_delegate dateView:self didSelectedDate:[self.datePicker date]];
    }
    
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0;
        
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
#pragma mark -- 点击取消按钮
- (void)cancelBtnClicked:(UIButton *)btn{
    if ([_delegate respondsToSelector:@selector(dateView:didSelectedDate:)]) {
        [_delegate dateView:self didSelectedDate:[self.datePicker date]];
    }
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0;
        
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];

    
}
@end
